/**
 * @Author:周邱艳
 * @Date:2020/6/30
 * @Description:这是交易记录页
 */
import React,{Component} from "react";
import {Icon, NavBar, Tabs, WhiteSpace, List, Button, Toast} from "antd-mobile";
import {URL} from '../../index'
import axios from "axios";
import {Link} from "react-router-dom";
import {getCookieId} from "../Login/Component_Login/GetCookie";
const Item = List.Item;
const Brief = Item.Brief;
var changeList=[];
var dealTime=[]
// 将获取的数据中属性为数字的转换为文字
function numberName(newList){
    for(let i=0;i<newList.length;i++) {
        //规范组合交易时间
        var StringTime=toString(newList[i].gmtModified)

        //修改组合交易状态
        switch (newList[i].tradeCategory) {
            case 0:
                newList[i].tradeCategory = "赎回确认中";
                break;
            case 1:
                newList[i].tradeCategory = "申购确认中";
                break;
            case 2:
                newList[i].tradeCategory = "申购成功";
                break;
            case 3:
                newList[i].tradeCategory = "撤单成功";
                break;
            case 4:
                newList[i].tradeCategory = "赎回成功";
                break;
            case 5:
                newList[i].tradeCategory = "提现成功";
                break;
            case 6:
                newList[i].tradeCategory = "充值成功";
                break;
        }
    }
    return newList;
}
//日期转字符串
function change(format) {
    var date = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S+": this.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
    }
    return format;
}

export default class TransactionRecord extends Component{
    constructor(props) {
        super(props);
        this.state = {
            tradeRecords:[],//json表名
            disabled: false,
            changeList:[],//对象内的数字转化为文字
            dealTime:[]//交易时间
        }
        this.loadData = this.loadData.bind(this)
        this.handleLeftClick = this.handleLeftClick.bind(this)
    }
    handleLeftClick(event){
        localStorage.setItem("tabs",2)
        this.props.history.push('/terran/my')
    }
    loadData(){
        axios({
            method:"GET",
            url:"http://39.101.141.248:9527/tradeRecord/tradeRecordLists",
            params:{userId:getCookieId()}//this.props.userId,this.props.tradeId
        }).then(res=>{
            changeList=numberName(res.data.tradeRecords)
            this.setState({
                changeList:changeList,
                dealTime:dealTime,
                tradeRecords:this.state.tradeRecords.concat(res.data.tradeRecords)
            })
        }).catch(error =>{
            console.log(error)
        })
    }
    //查看交易详情的回调
    seeDetail(){

    }
    componentDidMount() {
        this.loadData()
    }

    render() {
        return (
            <div>
                {/*页面顶部*/}
                <div style={{position:"fixed",width:"100%",zIndex:999}}>
                    <NavBar
                        mode="light"
                        icon={<Icon type="left" style={{color:" #f8a83f"}} />}
                        onLeftClick={this.handleLeftClick}
                    >交易记录</NavBar>
                </div>
                <WhiteSpace />
                <WhiteSpace />
                <WhiteSpace />
                <WhiteSpace size="lg" />

                <div>

                    <List renderHeader={() => '购买组合记录'} className="my-list">
                        {(()=>{return this.state.changeList.map((item,index)=>{
                            return(
                                <Link to={{pathname:"/transactionDetail",state:{tradeId:item.tradeId,tradeName:item.comBaseName,tradeAmoount:item.tradeAmoount,tradeCategory:item.tradeCategory,logicId:item.logicId,gmtModified:item.gmtModified}}}>
                                <Item
                                    multipleLine
                                    extra={
                                        <div>
                                        <p style={{marginBottom:13,fontSize:"18px",color:"red"}}>{item.tradeAmoount+"元"}</p>
                                        <p style={{color:"#f8a83f",fontSize:"18px"}}>{item.tradeCategory}</p>
                                        </div>}
                                    // onClick={this.seeDetail}
                                >
                                    {item.comBaseName}
                                    <Brief>
                                        {<div>
                                        <p>{"交易时间："+String(item.gmtModified).substring(0,String(item.gmtModified).length-2)}</p>
                                        <p>{"订单号："+item.logicId}</p>
                                        </div>}
                                    </Brief>
                                </Item>
                                </Link>
                            )
                        })
                        })()}
                    </List>

                </div>
            </div>
        );
    }
}
